//
//  BurglarHistoryVC.m
//  KEREN
//
//  Created by MW on 2023/4/21.
//

#import "BurglarHistoryVC.h"
#import "BurglarCell.h"
#import "BurglarModel.h"
@interface BurglarHistoryVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArrM;
@property (nonatomic, strong) BurglarModel *model;
@property (nonatomic, assign) int index;
@end

@implementation BurglarHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavViewWithRightTitle:@"" navColorIswhite:YES withTitle:@"上报历史"];
  //  [self tableView];
    _index = 1;
    [self getLoseList];
}

- (void)getLoseList{
    WEAKSELF
    [NetworkingTool POST:@"/v1/lose/list" parameters:@{@"index":@(_index),@"size":@(20)} isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];

        if([response[@"code"] intValue] == 0){
            if([response[@"data"][@"list"] isKindOfClass:[NSArray class]]){
                if(weakSelf.index == 1){
                    [weakSelf.dataArrM removeAllObjects];
                }
                NSArray *dataArr = response[@"data"][@"list"];
                for (int i = 0; i < dataArr.count; i ++) {
                    NSDictionary *dict = dataArr[i];
                    BurglarModel *model = [BurglarModel initWithDictionary:dict];
                    [weakSelf.dataArrM addObject:model];
                }
                if(weakSelf.dataArrM.count > 0){
                    weakSelf.tableView.tableHeaderView = nil;
                }else{
                    weakSelf.tableView.tableHeaderView = [[KRNodataView alloc] initWithFrame:CGRectZero];
                }
                if(dataArr.count < 20){
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                if (weakSelf.dataArrM.count == 0) {
                    [weakSelf.tableView.mj_footer setHidden:YES];
                }else{
                    [weakSelf.tableView.mj_footer setHidden:NO];
                }
                [weakSelf.tableView reloadData];
            }else{
                weakSelf.tableView.tableHeaderView = [[KRNodataView alloc] initWithFrame:CGRectZero];
                [weakSelf.tableView reloadData];

            }
        }
    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [UIView new];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BurglarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BurglarCell"];
    BurglarModel *model = self.dataArrM[indexPath.section];
    if(model.lose_type == 1){
        [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:model.ebike_image] placeholderImage:[UIImage imageNamed:@"devcie_defalaut_icon"] options:SDWebImageRefreshCached];
        cell.nameLbl.text = model.ebike_name;
        [cell.typeBtn setTitle:@"车辆失窃" forState:UIControlStateNormal];
    }else{
        cell.iconImage.image = [UIImage imageNamed:@"hueglarized_dc_icon"];
        cell.nameLbl.text = [NSString stringWithFormat:@"%@",model.battery_no];
        [cell.typeBtn setTitle:@"电池失窃" forState:UIControlStateNormal];
    }
    cell.timeLb.text = [NSString stringWithFormat:@"上报时间 %@",[[DeteManager shareInstance] timeFormatYYYYMMdd:model.updated_at]];
    cell.pointLbl.text = [NSString stringWithFormat:@"上报地点 %@",model.addr];
    NSAttributedString *showAttString = [NSAttributedString getAttributeWith:@[@"上报时间"] string:cell.timeLb.text Font:Font(12) Color:Black_Color attributeFont:Font_Medium(12) attributeColor:Color_86898E];
    cell.timeLb.attributedText = showAttString;
    
    NSAttributedString *pointAttString = [NSAttributedString getAttributeWith:@[@"上报地点"] string:cell.pointLbl.text Font:Font(12) Color:Black_Color attributeFont:Font_Medium(12) attributeColor:Color_86898E];
    cell.pointLbl.attributedText = pointAttString;
    if(model.status == 0){
        if(model.lock_status == 0){
            cell.statusLb.text = @"锁定中";
        }else{
            cell.statusLb.text = @"锁定成功";
        }
    }else if (model.status == 1){
        cell.statusLb.text = @"解除失窃成功";
    }else{
    }
    cell.statusLb.textColor = Color_7ACC00;

    cell.lineView.hidden = YES;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVTOP_Height, DEVICE_WIDTH, DEVICE_HEIGHT - NAVTOP_Height)];
        _tableView.backgroundColor = Main_BackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"BurglarCell" bundle:nil] forCellReuseIdentifier:@"BurglarCell"];
        WEAKSELF
        _tableView.mj_header =
        [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.index = 1;
            [self getLoseList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.index ++;
            [self getLoseList];

        }];
        _tableView.mj_footer.hidden = YES;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArrM{
    if(!_dataArrM){
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}

@end
